package com.chenchao.thread;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/17 18:59
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javathread.Test9.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 线程停止，推荐使用标识符或者有限循环让线程自己停下
 */
public class Test9 implements Runnable {
    private boolean flag = true;
    @Override
    public void run() {
        int i = 0;
        while (flag) {
            System.out.println(i++);
        }
    }
    public static void main(String[] args) {

        Test9 target = new Test9();
        Thread thread = new Thread(target);
        thread.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        target.setFlag(false);
    }
    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
